package com.itheima.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService{

    @Autowired
    private MenuDao menuDao;


    @Override
    public List<Menu> findAll() {
        List<Menu> menu = menuDao.findAll();
        return menu;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page = menuDao.findPage(queryString);
        return  new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 编辑：数据的回显
     * @param id
     * @return
     */
    @Override
    public Menu check(Integer id) {
        return menuDao.check(id);
    }

    /**
     * 编辑菜单
     * @param menu
     */
    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    /**
     * 添加菜单
     * @param menu
     */
    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    /**
     * 删除菜单项
     * @param id
     */
    @Override
    public void delete(Integer id) throws RuntimeException{
        //通过菜单id查询是否有与其它表的关联关系
        int count = menuDao.findRoleByMenuId(id);
        if (count > 0){
            throw new RuntimeException("当前所选菜单具有关联关系，不能删除");
        }
        menuDao.delete(id);
    }

}
